package com.certificationManager.client.view;

import java.util.Date;

import com.certificationManager.client.i18n.AppConstants;
import com.certificationManager.client.presenter.view_interface.ILoginView;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Benjamin Barbier
 *
 */
public class LoginView extends Composite implements ILoginView {
	
	/** Internationalization */
	AppConstants i18n = GWT.create(AppConstants.class);
	
	/** Fields */
	private final DockPanel initMainPage = new DockPanel();
	
	private final Button loginButton = new Button(i18n.login_Connection());
	private final TextField<String> emailField = new TextField<String>();
	private final TextField<String> passField = new TextField<String>();
	private final FlexTable loginLayout = new FlexTable();
	private final HorizontalPanel flagsLayout = new HorizontalPanel();
	private final DecoratorPanel loginPanel = new DecoratorPanel();
	private final DecoratorPanel premiersPasPanel = new DecoratorPanel();
	private final DecoratorPanel flagsPanel = new DecoratorPanel();
	private final VerticalPanel eastPanel = new VerticalPanel();
	private final SimplePanel northPanel = new SimplePanel();
	private final FlexTable southPanel = new FlexTable();
	private final FlexTable layoutPremiersPas = new FlexTable();
	private final Button startButton = new Button("Premiers pas");
	private final HTML htmlWest = new HTML();
	private final Label mainTitle = new Label();
	private final Image flagFR = new Image("includes/flags/FR.png");
	private final Image flagEN = new Image("includes/flags/EN.png");
	private final Image flagIT = new Image("includes/flags/IT.png");
	private final Image flagSP = new Image("includes/flags/SP.png");
	private final Image flagDE = new Image("includes/flags/DE.png");
	private final Image imageInfo = new Image("includes/info.png");
	
	@SuppressWarnings("deprecation") //for: new Date().getYear()
	public LoginView() {

		initMainPage.setWidth("94%");
		initMainPage.setSpacing(20);
		initMainPage.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		initMainPage.add(northPanel, DockPanel.NORTH);
		initMainPage.add(southPanel, DockPanel.SOUTH);
		initMainPage.add(htmlWest, DockPanel.WEST);
		htmlWest.setWidth("700px");
		eastPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		initMainPage.add(eastPanel, DockPanel.EAST);
		mainTitle.setStyleName("mainTitle");
		
		northPanel.setWidget(mainTitle);
		mainTitle.setText("Certification Manager");
		mainTitle.setHeight("70px");
		
		southPanel.setWidget(0,0, new Label("Certification Manager"));
		southPanel.setWidget(0, 1, imageInfo);
		southPanel.setWidget(1, 0, new Label("Copyright © 2005-"+(new Date().getYear()+1900)+". All Rights Reserved."));
		southPanel.getFlexCellFormatter().setColSpan(1, 0, 2);
		
		htmlWest.setHTML(
			"<h2 style='text-align:left;'>The standard Lorem Ipsum passage, used since the 1500s</h2>"+
			"<p style='text-align:left;'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>"+
			"<br />"+
			"<h2 style='text-align:left;'>Section 1.10.32 of 'de Finibus Bonorum et Malorum', written by Cicero in 45 BC</h2>"+
			"<p style='text-align:left;'>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>"
		);
		
		// Create a table to loginLayout the form options
	    loginLayout.setCellSpacing(6);
	    loginLayout.setWidth("350px");
	    layoutPremiersPas.setCellSpacing(6);
	    layoutPremiersPas.setWidth("350px");
	    
	    // Deco
	    loginPanel.setWidget(loginLayout);
	    premiersPasPanel.setWidget(layoutPremiersPas);
	    flagsLayout.setSpacing(10);
	    flagsPanel.setWidget(flagsLayout);
	    
	    // Flags
	    flagFR.setStyleName("gwt-Image-flag");
	    flagsLayout.add(flagFR);
	    flagEN.setStyleName("gwt-Image-flag");
	    flagsLayout.add(flagEN);
	    flagIT.setStyleName("gwt-Image-flag");
	    flagsLayout.add(flagIT);
	    flagSP.setStyleName("gwt-Image-flag");
	    flagsLayout.add(flagSP);
	    flagDE.setStyleName("gwt-Image-flag");
	    flagsLayout.add(flagDE);
	    
		// Login
	    loginLayout.setHTML(0, 0, i18n.login());
	    loginLayout.setWidget(0, 1, emailField);
	    loginLayout.setHTML(1, 0, i18n.pass());
	    loginLayout.setWidget(1, 1, passField);
	    passField.setPassword(true);
	    loginLayout.setWidget(2, 0, loginButton);
	    loginLayout.getFlexCellFormatter().setColSpan(2, 0, 2);

	    // Premiers pas
	    layoutPremiersPas.setHTML(0, 0, i18n.login_FirstStepLibelle());
	    layoutPremiersPas.setWidget(1, 0, startButton);
	    
	    eastPanel.add(loginPanel);
	    eastPanel.add(new HTML("<br />"));
	    eastPanel.add(premiersPasPanel);
	    eastPanel.add(new HTML("<br />"));
	    eastPanel.add(flagsPanel);
	    
	    initWidget(initMainPage);
	    
	}

	@Override
	public TextField<String> getEmailField() {
		return emailField;
	}

	@Override
	public Button getLoginButton() {
		return loginButton;
	}

	@Override
	public TextField<String> getPasswordField() {
		return passField;
	}

	@Override
	public Widget getViewWidget() {
		return this;
	}

	@Override
	public Button getStartButton() {
		return startButton;
	}
	
	@Override
	public HasClickHandlers getFlagFR() {
		return flagFR;
	}
	
	@Override
	public HasClickHandlers getFlagEN() {
		return flagEN;
	}
	
	@Override
	public HasClickHandlers getFlagIT() {
		return flagIT;
	}
	
	@Override
	public HasClickHandlers getFlagSP() {
		return flagSP;
	}
	
	@Override
	public HasClickHandlers getFlagDE() {
		return flagDE;
	}
	
	@Override
	public HasClickHandlers getImageInfo() {
		return imageInfo;
	}
	
}
